package cate.game.stat;

import cate.game.GameBody;
import cate.game.framework.ModuleMan;
import cate.game.role.Role;
import cate.game.stat.game.StatGameHandler;
import cate.game.stat.item.*;
import cate.game.stat.login.StatLoginHandler;
import cp.bi.mod.res.io.ResIo;
import cp.solution.business.sys.account.AccountEntity;

/** 统计管理 */
public class StatManager extends ModuleMan {

    public boolean disabled = false;

    public final StatGameHandler gameHandler = new StatGameHandler();
    private final StatLoginHandler loginHandler = new StatLoginHandler();
    private final StatMoneyHandler moneyHandler = new StatMoneyHandler();
    public final StatHeroPlusHandler heroPlus = new StatHeroPlusHandler();

    @Override
    public void initIndependent(GameBody game) throws Exception {
        super.initIndependent(game);
        gameHandler.initIndependent(game);
        loginHandler.initIndependent(game);
        moneyHandler.initIndependent(game);
        heroPlus.initIndependent(game);
    }

    @Override
    public void initDependent() throws Exception {
        gameHandler.initDependent();
        loginHandler.initDependent();
        moneyHandler.initDependent();
        heroPlus.initDependent();
    }

    public void every3Min() {
        moneyHandler.every3Min();
    }

    /** 游戏内存级重要数据监控 */
    public GameCT ct() {
        return new GameCT().init(this.game);
    }

    public void itemRecord(Role role, StatItemIOEntity entity) {
        if(disabled) { return; }
        entity.read(role);

        ResIo resIo = entity.toResIo();
        resIo.groupCode = game.config.gs.getGroupCode();
        resIo.gameId = role.getBase().gameId;
        game.bi().resIo.add(resIo);

        moneyHandler.recordItem(entity);
    }

    /** 当账户登录时 */
    public void whenAccountLogin(AccountEntity account) {
        loginHandler.whenAccountLogin(account);
    }

    /** 当角色登录时 */
    public void whenLogin(Role role) {
        loginHandler.whenRoleLogin(role);
    }

    /** 当角色上线时 */
    public void whenOnline(Role role) {
        game.bi().online.whenRoleOnline(role.getUid(), role.getBase().srvId, game.config.gs.getGroupCode(), role.getBase().gameId);
    }

    /** 当角色下线时 */
    public void whenOffline(Role role) {
        game.bi().online.whenRoleOffline(role.getUid(), role.getBase().srvId, game.config.gs.getGroupCode(), role.getBase().gameId);
    }

    public void tick(){
    }

    @Override
    public void shutdown(){
    }
}
